package step1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class ArtFont2 extends JFrame{
    JComboBox fontType;//字体样式下拉框
    JComboBox fontSize;//字体大小下拉框
    JComboBox windowStyle;//窗体样式下拉框
    JCheckBox boldBx;// 粗体按钮
    JCheckBox italicBx;// 斜体按钮
    JButton colorBtn;// 颜色按钮；
    String[] fontNames;// 字体名称;
    String[] fontSizes;// 字体大小；
    JLabel label1;// 输入提示标签；
    JLabel label2;// 学生个人信息标签；
    JTextField inputText;// 文字输入框；
    JTextArea txtArea;// 文字显示区;
    JPanel northPanel;// 字体设置；
    JPanel centerPanel;// 显示效果区
    JPanel southPanel;//样式设置
    Font font;
    int boldStyle, italicStyle, underlineStyle;
    int fontSizeStyle;
    String fontNameStyle;
    Color colorStyle = Color.black;// 设置字体的默认颜色为黑色;
    String[] style = {"默认显示效果", "Windows显示效果", "Unix显示效果"};

    public ArtFont2() {
        super("字体设置");
        // 设置默认字体
        boldStyle = 0;
        italicStyle = 0;
        underlineStyle = 0;
        fontSizeStyle = 10;
        fontNameStyle = "宋体";
        font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);

        northPanel = getNorthPanel();
        centerPanel = getCenterPanel();
        southPanel = getSouthPanel();
        // 设置容器;
        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        //将northPanel添加到窗体的北部
        container.add(northPanel, BorderLayout.NORTH);
        //将centerPanel添加到窗体的中部
        container.add(centerPanel, BorderLayout.CENTER);
        //将southPanel添加到窗体的南部
        container.add(southPanel, BorderLayout.SOUTH);
        setSize(500, 300);
        //将窗体位于屏幕的中央
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private JPanel getNorthPanel() {
        JPanel panel = new JPanel();
        label1 = new JLabel("输入");
        inputText = new JTextField(10);
        boldBx = new JCheckBox("粗体");
        italicBx = new JCheckBox("斜体");
        colorBtn = new JButton("颜色");
        label2 = new JLabel("计225 201902049 朱晓龙");
        //2：匿名类做监视器
        //注册监视器
        inputText.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                txtArea.setText(inputText.getText());
            }
        });
        colorBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                colorStyle = JColorChooser.showDialog(null, "请选择一种颜色", colorStyle);
                colorBtn.setForeground(colorStyle);
                txtArea.setForeground(colorStyle);
            }
        });
        boldBx.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (boldBx.isSelected()) {
                    boldStyle = Font.BOLD;
                } else {
                    boldStyle = Font.PLAIN;
                }
                font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);
                txtArea.setFont(font);
            }
        });
        italicBx.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (italicBx.isSelected()) {
                    italicStyle = Font.ITALIC;
                } else {
                    italicStyle = Font.PLAIN;
                }
                font = new Font(fontNameStyle, boldStyle + italicStyle, fontSizeStyle);
                txtArea.setFont(font);
            }
        });

        panel.add(label1);
        panel.add(inputText);
        panel.add(boldBx);
        panel.add(italicBx);
        panel.add(colorBtn);
        panel.add(label2);
        return panel;
    }

    private JPanel getCenterPanel() {
        JPanel panel = new JPanel();
        txtArea = new JTextArea(10, 10);
        panel.setLayout(new BorderLayout());
        panel.add(new JScrollPane(txtArea), BorderLayout.CENTER);
        return panel;
    }

    private JPanel getSouthPanel() {
        JPanel panel = new JPanel();
        //获得系统默认字体
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        fontNames = ge.getAvailableFontFamilyNames();
        fontType = new JComboBox(fontNames);

        //设置字体大小
        fontSizes = new String[63];
        for (int i = 0; i < fontSizes.length; i++) {
            fontSizes[i] = Integer.toString(i + 10);
        }
        fontSize = new JComboBox(fontSizes);
        windowStyle = new JComboBox(style);
        //注册监视器
        fontType.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                fontNameStyle = (String) (e.getItem());
            }
        });
        fontSize.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                fontSizeStyle = Integer.parseInt((String) (e.getItem()));
            }
        });
        windowStyle.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                String s = (String) e.getItem();
                String className = "";
                if (s.equals("Windows显示效果"))
                    className = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
                else if (s.equals("Unix显示效果"))
                    className = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
                else if (s.equals("默认显示效果"))
                    className = UIManager.getCrossPlatformLookAndFeelClassName();
                try {
                    UIManager.setLookAndFeel(className);
                    SwingUtilities.updateComponentTreeUI(null);
                } catch (Exception de) {
                    System.out.println("Exception happened!");
                }
            }
        });

        panel.add(fontType);
        panel.add(fontSize);
        panel.add(windowStyle);
        return panel;
    }

    public static void main(String args[]) {
        ArtFont artFont = new ArtFont();
        artFont.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}